package core

import (
	"fmt"

	"gitee.com/whoiszhangben/hj212_recSvc/core/hj212"
	"gitee.com/whoiszhangben/hj212_recSvc/core/hj212/protocol"
	"gitee.com/whoiszhangben/hj212_recSvc/global"

	"go.uber.org/zap"
)

func RunServer() {
	listenAddress := fmt.Sprintf("0.0.0.0:%d", global.GVA_CONFIG.System.Addr)
	server := hj212.NewServer(hj212.Option{
		ListenAddress: listenAddress,
		SendChanSize:  0,
		Keepalive:     0,
		CloseHandler:  nil,
	})
	server.AddHandler(uint16(protocol.Cn_2011), handleRealTimeData)
	server.AddHandler(uint16(protocol.Cn_2051), handleMiuteData)
	server.AddHandler(uint16(protocol.Cn_2061), handleHourData)
	server.AddHandler(uint16(protocol.Cn_2031), handleDayData)

	if err := server.Run(); err != nil {
		global.GVA_LOG.Error("服务启动失败", zap.String("desc", err.Error()))
	}
}

func handleRealTimeData(session *hj212.Session, message *protocol.Message) {

	entity, ok := message.Body.(*protocol.HJ212_2011)
	if !ok {
		global.GVA_LOG.Error("data type error")
		return
	}

	session.DataReply(*message)
	qn := message.Header.QN
	st := message.Header.ST
	cn := message.Header.CN
	mn := message.Header.MN

	var results []*global.HJ212Data

	for key, value := range entity.Data {
		results = append(results, &global.HJ212Data{
			QNTime:     qn,
			DataTime:   entity.DataTime,
			MN:         mn,
			ST:         st,
			CN:         cn,
			FactorCode: key,
			RtdValue:   &value.Rtd,
			Flag:       value.Flag,
		})
	}

	result := global.GVA_DB.CreateInBatches(results, 100)
	if result.Error != nil {
		global.GVA_LOG.Error("data type error")
	}
	global.GVA_LOG.Info("Successfully inserted", zap.Int64("records:", result.RowsAffected))

}

func handleMiuteData(session *hj212.Session, message *protocol.Message) {

	entity, ok := message.Body.(*protocol.HJ212_2051)
	if !ok {
		global.GVA_LOG.Error("data type error")
		return
	}

	session.DataReply(*message)
	mn := message.Header.MN
	qn := message.Header.QN
	st := message.Header.ST
	cn := message.Header.CN

	var results []*global.HJ212Data

	for key, value := range entity.Body {
		results = append(results, &global.HJ212Data{
			QNTime:     qn,
			DataTime:   entity.DataTime,
			MN:         mn,
			ST:         st,
			CN:         cn,
			FactorCode: key,
			RtdValue:   &value.Avg,
			Flag:       value.Flag,
		})
	}

	result := global.GVA_DB.CreateInBatches(results, 100)
	if result.Error != nil {
		global.GVA_LOG.Error("data type error")
	}
	global.GVA_LOG.Info("minute Successfully inserted", zap.Int64("records:", result.RowsAffected))

}

func handleHourData(session *hj212.Session, message *protocol.Message) {

	entity, ok := message.Body.(*protocol.HJ212_2061)
	if !ok {
		global.GVA_LOG.Error("data type error")
		return
	}

	session.DataReply(*message)
	mn := message.Header.MN
	qn := message.Header.QN
	st := message.Header.ST
	cn := message.Header.CN

	var results []*global.HJ212Data

	for key, value := range entity.Data {
		results = append(results, &global.HJ212Data{
			QNTime:     qn,
			DataTime:   entity.DataTime,
			MN:         mn,
			ST:         st,
			CN:         cn,
			FactorCode: key,
			RtdValue:   &value.Avg,
			Flag:       value.Flag,
		})
	}

	result := global.GVA_DB.CreateInBatches(results, 100)
	if result.Error != nil {
		global.GVA_LOG.Error("data type error")
	}
	global.GVA_LOG.Info("hour Successfully inserted", zap.Int64("records:", result.RowsAffected))

}

func handleDayData(session *hj212.Session, message *protocol.Message) {

	entity, ok := message.Body.(*protocol.HJ212_2031)
	if !ok {
		global.GVA_LOG.Error("data type error")
		return
	}

	session.DataReply(*message)
	mn := message.Header.MN
	qn := message.Header.QN
	st := message.Header.ST
	cn := message.Header.CN

	var results []*global.HJ212Data

	for key, value := range entity.Data {
		results = append(results, &global.HJ212Data{
			QNTime:     qn,
			DataTime:   entity.DataTime,
			MN:         mn,
			ST:         st,
			CN:         cn,
			FactorCode: key,
			RtdValue:   &value.Avg,
			Flag:       value.Flag,
		})
	}

	result := global.GVA_DB.CreateInBatches(results, 100)
	if result.Error != nil {
		global.GVA_LOG.Error("data type error")
	}
	global.GVA_LOG.Info("day Successfully inserted", zap.Int64("records:", result.RowsAffected))

}
